package cn.iocoder.yudao.module.carbon.service.gwp;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.carbon.api.gwp.dto.GwpListReqDTO;
import cn.iocoder.yudao.module.carbon.controller.admin.gwp.vo.GwpCreateReqVO;
import cn.iocoder.yudao.module.carbon.controller.admin.gwp.vo.GwpExportReqVO;
import cn.iocoder.yudao.module.carbon.controller.admin.gwp.vo.GwpPageReqVO;
import cn.iocoder.yudao.module.carbon.controller.admin.gwp.vo.GwpUpdateReqVO;
import cn.iocoder.yudao.module.carbon.convert.gwp.GwpConvert;
import cn.iocoder.yudao.module.carbon.dal.dataobject.gwp.GwpDO;
import cn.iocoder.yudao.module.carbon.dal.mysql.gwp.GwpMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.carbon.enums.ErrorCodeConstants.GWP_NOT_EXISTS;

/**
 * 全球变暖潜能值 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class GwpServiceImpl implements GwpService {

    @Resource
    private GwpMapper gwpMapper;

    @Override
    public Long createGwp(GwpCreateReqVO createReqVO) {
        // 插入
        GwpDO gwp = GwpConvert.INSTANCE.convert(createReqVO);
        gwpMapper.insert(gwp);
        // 返回
        return gwp.getId();
    }

    @Override
    public void updateGwp(GwpUpdateReqVO updateReqVO) {
        // 校验存在
        validateGwpExists(updateReqVO.getId());
        // 更新
        GwpDO updateObj = GwpConvert.INSTANCE.convert(updateReqVO);
        gwpMapper.updateById(updateObj);
    }

    @Override
    public void deleteGwp(Long id) {
        // 校验存在
        validateGwpExists(id);
        // 删除
        gwpMapper.deleteById(id);
    }

    private void validateGwpExists(Long id) {
        if (gwpMapper.selectById(id) == null) {
            throw exception(GWP_NOT_EXISTS);
        }
    }

    @Override
    public GwpDO getGwp(Long id) {
        return gwpMapper.selectById(id);
    }

    @Override
    public List<GwpDO> getGwpList(Collection<Long> ids) {
        return gwpMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<GwpDO> getGwpPage(GwpPageReqVO pageReqVO) {
        return gwpMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GwpDO> getGwpList(GwpExportReqVO exportReqVO) {
        return gwpMapper.selectList(exportReqVO);
    }

    @Override
    public List<GwpDO> getGwpList(GwpListReqDTO reqDTO) {
        return gwpMapper.selectList(reqDTO);
    }
}
